import { defineStore } from "pinia";

import {
  reqCategory1List,
  reqCategory2List,
  reqCategory3List,
} from "@/api/product/attr";

import type { ICategoryStore } from "./interface/category";

const categoryStore = defineStore("categoryStore", {
  state: (): ICategoryStore => ({
    category1List: [], // 一级分类数据
    category1Id: "", // 一级分类的ID
    category2List: [], // 二级分类数据
    category2Id: "", // 二级分类的ID
    category3List: [], // 三级分类数据
    category3Id: "", // 三级分类的ID
  }),
  actions: {
    // 获取一级分类数据
    async getCategory1List() {
      const result = await reqCategory1List();
      this.category1List = result;
    },
    // 获取二级分类数据
    async getCategory2List() {
      this.category2List = [];
      this.category2Id = "";
      this.category3List = [];
      this.category3Id = "";
      const result = await reqCategory2List(this.category1Id);
      this.category2List = result;
    },
    // 获取三级分类数据
    async getCategory3List() {
      this.category3List = [];
      this.category3Id = "";
      const result = await reqCategory3List(this.category2Id);
      this.category3List = result;
    },
    // 切换界面清除分类数据
    deleteCategory() {
      this.category1Id = "";
      this.category1List = [];
      this.category2Id = "";
      this.category2List = [];
      this.category3Id = "";
      this.category3List = [];
    },
  },
});

export default categoryStore;
